package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.OmsOrder;
import com.itheima.mapper.OmsOrderMapper;
import com.itheima.service.OmsOrderService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author zhuguo
 * @since 2022-11-10
 */
@DubboService
public class OmsOrderServiceImpl extends ServiceImpl<OmsOrderMapper, OmsOrder> implements OmsOrderService {

    @Autowired
    OmsOrderMapper omsOrderMapper;

    //分页查询订单详情
    @Override
    public PageBeanVo findByPage
    (String createTime, String orderSn, Integer orderType, Integer pageNum,
     Integer pageSize, String receiverKeyword, Integer sourceType, Integer status) {
        Page<OmsOrder> page = new Page<>(pageNum,pageSize);

        LambdaQueryWrapper<OmsOrder> qw = new LambdaQueryWrapper<>();



        qw.like(orderSn != null, OmsOrder::getOrderSn, orderSn);
        qw.like(receiverKeyword != null, OmsOrder::getReceiverName, receiverKeyword).
                or().like(receiverKeyword!=null,OmsOrder::getReceiverPhone, receiverKeyword);
        qw.eq(createTime != null, OmsOrder::getCreateTime, createTime);
        qw.eq(orderType != null, OmsOrder::getOrderType, orderType);
        qw.eq(sourceType != null, OmsOrder::getSourceType, sourceType);
        qw.eq(status != null, OmsOrder::getStatus, status);
        qw.orderByDesc(OmsOrder::getCreateTime);

        Page<OmsOrder> omsOrderPage = omsOrderMapper.selectPage(page,qw);

        return new PageBeanVo(pageNum,pageSize,omsOrderPage.getTotal(),omsOrderPage.getRecords());
    }

    //修改订单费用信息
    @Override
    public Integer updateOrder(OmsOrder order) {
        return omsOrderMapper.updateById(order);
    }

    //根据订单id查询指定订单详情
    @Override
    public OmsOrder findById(Long id) {
        return omsOrderMapper.selectById(id);
    }

    //根据订单id集合查询订单详情
    @Override
    public List<OmsOrder> findByIds(List<Long> ids) {
        return omsOrderMapper.selectBatchIds(ids);
    }

    //批量更新订单
    @Override
    public Integer updateOrderList(List<OmsOrder> orderList) {
        Integer count = null;
        for (OmsOrder order : orderList) {
            count = omsOrderMapper.updateById(order);
        }
        return count;
    }
}

